﻿using BLL.AntiFake;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NbSecurityAppManage.AntiFake
{
    public partial class Default : System.Web.UI.Page
    {
        DigitCodeBLL digitCodeBLL = new DigitCodeBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = Request["type"] != "" ? Request["type"] : "0";
            string code = Request["code"] != "" ? Request["code"] : "0";
            if (type == "1")
            {
                Response.Write(jsons(code));
            }
        }

        public string jsons(string code)
        {
            
            
            if (code != string.Empty)
            {
                
                DataTable dt = digitCodeBLL.getQueryLog(code);
                return DataTable2Json(dt);
            }
            else
            {
                return "200";
            }
        }

        public static string DataTable2Json(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{\"Table");
            jsonBuilder.Append(dt.TableName);
            jsonBuilder.Append("\":[");
            jsonBuilder.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }
    }
}